package com.bw.documents.manager

import com.bw.common.mvi.BaseRespository
import com.bw.network.BaseEntity
import com.bw.network.HttpType
import com.bw.network.NetWorkFactory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class DocumentsRepo : BaseRespository() {

    //查询文件
    fun selDocuments(map : Map<String,Any>):Flow<BaseEntity>{
        return NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .requestSelDocuments(createBody(map))
    }

    //添加文件
    fun insertDocuments(map : Map<String,Any>):Flow<BaseEntity>{
        return NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .requestInsertDocuments(createBody(map))
            .map { value ->  value}
    }
    //上传文件
    fun uploadDocuments(map: Map<String, Any>):Flow<BaseEntity>{
        val path : String = map["path"] as String
        val name : String = map["name"] as String
        val body : RequestBody = RequestBody.create(
            MediaType.parse("multipart/form-data"), File(path))
        val multiPartBuilder = MultipartBody.Builder()
        multiPartBuilder.addFormDataPart("file",name,body)
        return NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .requestUploadDocuments(multiPartBuilder.build().parts())
    }
    //删除文件
    fun delDocuments(map : Map<String,Any>):Flow<BaseEntity>{
        return NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .requestDelDocuments(createBody(map))
    }
    //同步文件
    fun syncDocuments(map : Map<String,Any>):Flow<BaseEntity>{
        return NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .requestSyncDocuments(createBody(map))
    }
}